<?php
/**
 * Defines Kodform_Element_Field class
 *
 *
 * Kodform.php - kodform4
 * @copyright 	Copyright (c) 2006 Boris Tomic (kodmasin.net)
 * @category 	Kodform
 * @package 	Kodform
 * @author 		Boris Tomic
 * @license 	http://boris.kodmasin.net/kodform4:license The BSD License
 *
 */
 
require_once 'Kodform/Element.php';
require_once 'Kodform/Attributes.php';
/**
 * this is implemetation of text (password) field (Kod_Form_Element) 
 */
class Kodform_Element_Field extends Kodform_Element{
	/**
	 * @var string holds type (text or password or hidden)
	 */
	protected $type;
	/**
	 * 
	 * var integersize attribute used when displaying this element
	 */
	protected $displaySize;
	/**
	 * @var integer maxsize atribute used when displaying this element
	 */
	protected $maxSize;
	
	const text = "text";
	const password = "password";
	const hidden = "hidden";
	/**
	 * create field object
	 * 
	 * @param string $id id of element @see Kod_Form_Element::id
	 * @param string $displayName field name which will be displayed
	 * @param string $type type of this element (text, password, hidden). See
	 * constants of this class
	 * @param string $defaultValue value which will be displayed to user in this
	 * element. If form is submited submited value will be displayed.
	 * @param string $required if this is set than the element will be required and the suplied text will
	 * be error message for validation (if field do not contain value)
	 */
	function __construct($id, $displayName, $type = Kodform_Element_Field::text, $defaultValue = null, $required = null){
		parent::__construct($id, $defaultValue, $required);
		$this->type = $type;
		$this->displaySize = 20;
		$this->maxSize = 0;
		$this->displayName = $displayName;
	}
	/**
	 * default display function
	 */
	function display(Kodform_Attributes $attributes = null){
		$value = $this->getValue();
		$str = "<input id=\"" . $this->id . "\" name=\"" . $this->id . "\" type=\"" . $this->type . "\" value=\"" . $value . "\""; 
		
		if($this->type != Kodform_Element_Field::hidden){
			if($attributes == null)
				$attributes = new Kodform_Attributes();
			$attr = $attributes->normal;
			if($this->isRequired())
				$attr = $attributes->required;
			if(!$this->isValid())
				$attr = $attributes->error;
			$str .= $attr->display();
			
			$str .= " size=\"" . $this->displaySize . "\"";
			
			if($this->type == Kodform_Element_Field::text && $this->maxSize > 0)
				$str .= " maxlength=\"" . $this->maxSize . "\"";
		}
		
		$str .= " />";
		return $str;
	}
	/**
	 * set size
	 * 
	 * this sets attribute size of this field (html)
	 */
	function setDisplaySize($size){
		$this->displaySize = $size;
	}
	/**
	 * set maxsize 
	 * 
	 * this sets maxsize attribute of this field (html)
	 */
	function setMaxSize($maxsize){
		$this->maxSize = $maxsize;
	}
}
